﻿#if !Profile && __TRUNKTQ
using System;
using System.Diagnostics;
using O1.Kernel;
using O1.Kernel.Storage;
using O1.Kernel.Storage.Transactions;

namespace O1.Profiling
{
    class Program
    {
        private const int size = 1024 * 1024 + 1000;

        static void Main(string[] args)
        {
            var bytesAllocated = GC.GetTotalMemory(true);
            var stopwatch = Stopwatch.StartNew();

            var trunk = new __Trunk(size, 0);

            stopwatch.Stop();
            Console.WriteLine("");
            var difference = GC.GetTotalMemory(true) - bytesAllocated;
            Console.WriteLine("Allocated " + size + " trunk storage.");
            Console.WriteLine("...in " + stopwatch.ElapsedMilliseconds + " milliseconds.");

            stopwatch.Restart();
            using (var token = new ConcurrentScope())
            {
                for (int i = 0; i < size; i++)
                {

                    trunk.AllocateVertex((i % 2 == 0) ? new byte[256] : new byte[256], token, token.TransactionId - 1);
                }

                trunk.TryCommit(token);
            }

            using (var token = new ConcurrentScope())
            {
                for (int i = 1; i < size + 1; i++)
                {
                    var newVersion = trunk.ReadVertex(i, token, token.TransactionId - 1);
                    newVersion[0] = 255;
                    trunk.WriteVertex(i, newVersion, token, token.TransactionId - 1);
                }
            }

            using (var token = new ConcurrentScope())
            {
                for (int i = 1; i < size + 1; i++)
                {
                    var newVersion = trunk.ReadVertex(i, token, token.TransactionId - 1);
                    newVersion[0] = 255;
                    trunk.WriteVertex(i, newVersion, token, token.TransactionId - 1);
                }

                trunk.TryCommit(token);
            }

            stopwatch.Stop();
            Console.WriteLine("Processed " + size + " cells in " + stopwatch.ElapsedMilliseconds + " milliseconds.");
            Console.WriteLine("... " + stopwatch.ElapsedMilliseconds / size + " milliseconds per cell.");
            Console.ReadLine();
        }
    }
}

#endif